<?php

namespace App\WeChat;

use App\Http\Controllers\Controller;
use App\Http\Requests\WeChatRequest;
use App\Http\Resources\WeChatResource;
use App\Models\Site;
use App\Models\WeChat;
use Illuminate\Http\Request;

class WeChatController extends Controller
{
    public function __construct()
    {
        $this->middleware(['auth:sanctum', 'site']);
        $this->authorizeResource(Wechat::class, 'wechat');
    }


    public function index(Site $site)
    {
        return WeChatResource::collection($site->wechats);
    }


    public function store(WeChatRequest $request, Site $site, WeChat $wechat)
    {
        $wechat->fill($request->input() + ['site_id' => $site->id])->save();
        return $this->message('微信添加成功');
    }

    public function show(Site $site, WeChat $wechat)
    {
        return new WeChatResource($wechat);
    }


    public function update(WeChatRequest $request, Site $site, WeChat $wechat)
    {
        $wechat->fill($request->input())->save();
        return $this->message('微信修改成功');
    }

    public function destroy(Site $site, WeChat $wechat)
    {
        $wechat->delete();
        return $this->message('删除成功');
    }
}
